﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimpleEncryption.Contract;

namespace SimpleEncryption.DecriptionSteps
{
    /// <summary>
    /// This is a very simple Letter Replacment Algorithm
    /// </summary>
    public class LetterReplacement : ICryptoStep
    {
        /// <summary>
        /// Gets or sets the letter releations
        /// Inside the values the letters also have to be unique
        /// </summary>
        public Dictionary<char, char> LetterReleations { get; set; }

        private Dictionary<char, char> letterRelationReveresed;


        /// <summary>
        /// Gets the letter relation reveresed.
        /// </summary>
        private Dictionary<char, char>  LetterRelationReveresed
        {
            get
            {
                if (letterRelationReveresed == null)
                {
                    letterRelationReveresed = new Dictionary<char, char>();
                    foreach (var letterReleation in LetterReleations)
                    {
                        letterRelationReveresed.Add(letterReleation.Value, letterReleation.Key);
                    }
                }
                return letterRelationReveresed;
            }
        }

        public LetterReplacement()
        {
            LetterReleations = new Dictionary<char, char>();
        }

        /// <summary>
        /// Encrypts the specified text with an fixed algorithm
        /// </summary>
        /// <param name="text">The text to encrypt</param>
        /// <param name="password">The password.</param>
        /// <returns>
        /// The encrypted text
        /// </returns>
        public string Encrypt(string text, string password)
        {
            StringBuilder encrypted = new StringBuilder();
            foreach (char character in text)
            {
                if (LetterReleations.ContainsKey(character))
                {
                    encrypted.Append(LetterReleations[character]);
                }
                else
                {
                    encrypted.Append(character);
                }
            }
            return encrypted.ToString();
        }

        /// <summary>
        /// Decrypts the specified text with an fixed algortihm
        /// </summary>
        /// <param name="text">The text to decrypt</param>
        /// <param name="password">The password.</param>
        /// <returns>
        /// the decrypted text
        /// </returns>
        public string Decrypt(string text, string password)
        {
            StringBuilder decrypted = new StringBuilder();
            foreach (char character in text)
            {
                if (LetterRelationReveresed.ContainsKey(character))
                {
                    decrypted.Append(LetterRelationReveresed[character]);
                }
                else
                {
                    decrypted.Append(character);
                }
            }
            return decrypted.ToString();
        }
    }
}
